///////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2007-2009 ILOG, S.A.
//  All Rights Reserved.
//  The following is ILOG Source Code.  No modifications may be made to the  
//  ILOG Source Code.  Any usage of the ILOG Source Code is subject to 
//  the terms and conditions of the ILOG End User License Agreement   
//  applicable to this ILOG software product.
//
///////////////////////////////////////////////////////////////////////////////

package ilog.gauges.rectangular
{
  import ilog.gauges.gaugesClasses.CategoryScale;
  import ilog.gauges.gaugesClasses.rectangular.IRectangularScale;
 
  /**
   * The <code>RectangularCategoryScale</code> class represents the category scale for 
   * rectangular gauges. This class can be used to populate the <i>scales</i> array of 
   * the gauge. Categories are defined by the <i>categories</i> array. 
   *  
   * @see ilog.gauges.rectangular.RectangularGauge
   * 
   * @includeExample examples/RectangularCategoryScaleExample.mxml -noswf
   */
  public class RectangularCategoryScale extends CategoryScale implements IRectangularScale
  {
    private var _offset:Number = 0;

    [Bindable]
    [Inspectable(environment="none")]
        
    /**
     *  @copy ilog.gauges.gaugesClasses.rectangular.IRectangularScale#offset
     */
    public function get offset():Number
    {
      return _offset;
    }

    /**
     * @private
     */
    public function set offset(value:Number):void
    {
      if (value != _offset) {
        _offset = value;
        invalidateElements();
      }
    }

    private var _length:Number = 0;

    [Bindable]
    [Inspectable(environment="none")]    

    /**
     *  @copy ilog.gauges.gaugesClasses.rectangular.IRectangularScale#length
     */
    public function get length():Number
    {
      return _length;
    }

    /**
     * @private
     */
    public function set length(value:Number):void
    {
      if (value != _length) {
        _length = value;
        invalidateElements();
      }
    }

    /**
     *  @copy ilog.gauges.gaugesClasses.rectangular.IRectangularScale#positionForValue
     */
    public function positionForValue(value:Object):Number
    {
      if (value == null)
        return _offset;
        
      var v:String = String(value);
     
      var i:int = getCategoryIndex(v);
  
      if (i != -1)
        return _offset + (majorTickItems[i].position * _length);
      else
        return _offset; 
    }

    /**
     *  @copy ilog.gauges.gaugesClasses.rectangular.IRectangularScale#valueForPosition
     */
    public function valueForPosition(position:Number):Object
    {
      var ticks:Array = majorTickItems;
      var max:Number = ticks.length - 1;
      // limit positions, return the extremum
      if (position < _offset)
        return ticks[0].value;
      if (position > _offset + _length)
        return ticks[max].value;
      return ticks[Math.round((position - _offset) * max / _length)].value;
    }
    
    /**
     *  @copy ilog.gauges.gaugesClasses.rectangular.IRectangularScale#positionInRange
     */
    public function positionInRange(position:Number):Boolean
    {
      return (position >= _offset && position <= _offset + _length);
    }           
  }
}
